//
//  HTTPClient.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/16.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking

//线上环境 
private let hostname = "https://api.weibo.com/"
//测试环境
//private let hostname = "https://.dev.api.weibo.com/"

//** 反转域名 **
private let domain = "com.baidu.data.error"

enum HTTPMethods: String {

    case POST = "POST"
    case GET = "GET"
}

class HTTPClient: AFHTTPSessionManager {
    
// static let sharedhttps: HTTPClient = HTTPClient(baseURL: <#T##NSURL?#>, sessionConfiguration: <#T##NSURLSessionConfiguration?#>)
    
    //使用单例对象来访问 封装的核心方法 伪单例
    static let sharedHttpClient: HTTPClient = {
     
        //基地址 可以自动拼接前面的
        let url = NSURL(string: hostname)
        let tools = HTTPClient(baseURL:url)
        //设置支持的序列化格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //基于网络请求的封装到一个核心方法中
    func requestJsonDict(method: HTTPMethods,urlString: String,parameters: [String : AnyObject]?,finished: (dict: [String : AnyObject]?,error: NSError?) -> () ) {
    
        if method == .POST {
        
            //发起POST请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
                print(result)
                
                guard let dict = result as? [String : AnyObject] else {
                
                    print("数据结构错误")
                    //自定义错误信息
                    //错误的code 系统都是10000以内 若自定义 尽量不在10000以内
                    let myError = NSError(domain: domain, code: 10001, userInfo: [NSLocalizedDescriptionKey : "数据结构错误"])
                    finished(dict: nil, error: myError)
                    
                    return
                }
                //完成成功的回调
                finished(dict: dict, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    finished(dict: nil, error: error)
                    print(error)
            })
            
        }
        else {
            
            GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
                
                
                print(result)
                
                guard let dict = result as? [String : AnyObject] else {
                    
                    print("数据结构错误")
                    //自定义错误信息
                    //错误的code 系统都是10000以内 若自定义 尽量不在10000以内
                    let myError = NSError(domain: domain, code: 10001, userInfo: [NSLocalizedDescriptionKey : "数据结构错误"])
                    finished(dict: nil, error: myError)
                    
                    return
                }
                //完成成功的回调
                finished(dict: dict, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    finished(dict: nil, error: error)
                    print(error)
            })
            
            
        }

        
    
    }
    
    

}
